package Gather.StreamExercise.MethodReference.MethodDome;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * ClassName:MethodReference00
 * Package:Gather.StreamExercise.MethodReference
 * Description:
 *
 * @Author:ZhangXin
 * @Create2023/2/25-14:12
 * @Version:v1.0
 */
public class MethodReference004 {
    //3.引用构造方法
    //格式：类名::new
    //范例：Student::new
    //目的：创建这个类的对象

    /** 方法引用规则
     * 1.引用处必须是函数式接口
     * 2.被引用的方法必须已经存在
     * 3.被引用方法的形参和返回值需要跟抽象方法保持一致
     * 4.被引用方法的功能要满足当前需求*/

    /*练习
    * 集合里面存储姓名和年龄
    * 要求：将数据封装成Student对象并收集到集合List集合中*/
    public static void main(String[] args) {
        ArrayList<String> list = new ArrayList<>();

        Collections.addAll(list,"蔡坤坤,24","叶齁咸,23","刘不甜,22","吴谦,24","肖亮亮,27","谷加,30");

        //将数据封装成Student对象并收集到集合List集合中
        //String -->Student
//        List<Student> newList = list.stream().map(new Function<String, Student>() {
//            @Override
//            public Student apply(String s) {
//                String name = s.split(",")[0];
//                int age = Integer.parseInt(s.split(",")[1]);
//                return new Student(name, age);
//            }
//        }).collect(Collectors.toList());
//        System.out.println(newList);


        //方法引用
        List<Student> newList = list.stream().map(Student::new).collect(Collectors.toList());
        for (Student student : newList) {
            System.out.println(student);
        }

    }

}
